﻿using System;
using System.Text;
using System.Xml;
using IAM.Interface.Model.ENUM;
using IAM.Interface.Model;

namespace IAM.Interface.Common.XMLResolution
{
    /// <summary>
    /// xml请求包
    /// </summary>
    public class Request : AbstractXMLResolution
    {
        #region 构造函数
        public Request()
        { }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="actiontype">操作类型</param>
        /// <param name="operationtype">身份信息管理类型</param>
        /// <param name="apptype">传送参数</param>
        /// <param name="xmlinfo">对象的xml主体信息</param>
        public Request(
                        ActionType actiontype, 
                        OperationType operationtype,
                        TransmissionParameter tranpar,
                        string xmlinfo)
        {
            _actiontype = actiontype;
            _operationtype = operationtype;
            _tranpar = tranpar;
            _xmlinfo = xmlinfo;
        }
        #endregion 
        #region 属性
        private ActionType _actiontype = ActionType.Nullity;
        private OperationType _operationtype = OperationType.Nullity;
        private TransmissionParameter _tranpar = null;
        private string _transactionid = "";

        /// <summary>
        /// 操作类型
        /// </summary>
        public ActionType ActionType
        {
            get
            {
                if (_actiontype == ActionType.Nullity)
                {
                    return GetActionType();
                }
                else
                {
                    return _actiontype;
                }
            }
            set
            {
                _actiontype = value;
            }
        }
        /// <summary>
        /// 身份信息管理类型
        /// </summary>
        public OperationType OperationType
        {
            get
            {
                if (_operationtype == OperationType.Nullity)
                {
                    return GetOperationType();
                }
                else
                {
                    return _operationtype;
                }
            }
            set
            {
                _operationtype = value;
            }
        }
     
        public string TransactionID
        {
            get
            {
                if (_transactionid == "")
                {
                    return GetTransactionID();
                }
                else
                {
                    return _transactionid;
                }
            }
            set
            {
                _transactionid = value;
            }
        }
        public TransmissionParameter TranPar
        {
            get
            {
                return _tranpar;
            }
            set
            {
                _tranpar = value;
            }
        }
        #endregion
        #region 方法
        #region 获取当前结点
        /// <summary>
        /// 获取当前结点
        /// </summary>
        /// <returns></returns>
        public override XmlNode GetCurrentNode()
        {
            XmlElement xe = XmlDocument.DocumentElement;
            XmlNode xn = null;
            string strActionType = xe.GetAttribute("ActionType");
            switch (strActionType)
            {
                case "IDEN_MANAGE"://身份信息管理
                    xn = xe.SelectSingleNode("Identity");
                    break;
                case "IDEN_GETACMODEL"://获取权限模型
                    xn = (XmlNode)xe;
                    break;
                case "IDEN_GETUSERACI"://获取用户权限信息
                    xn = xe.SelectSingleNode("AccountInfo");
                    break;
                case "IDEN_SETUSERACI"://设置用户权限信息
                    xn = (XmlNode)xe;
                    break;
                case "ORG_UPDATE"://组织信息更新
                    xn = (XmlNode)xe;
                    break;
                case "ORG_CREATE"://组织机构创立
                    xn = (XmlNode)xe;
                    break;
                case "ORG_DELETE"://组织结构删除（包括合并）
                    xn = (XmlNode)xe;
                    break;
                case "DM_GETMODULELIST"://获取可代理模块列表
                    xn = (XmlNode)xe;
                    break;
                case "DM_GETDELEINFO"://获取用户代理信息
                    xn = xe.SelectSingleNode("AccountInfo");
                    break;
                case "DM_SETDELEINFO"://设置用户代理信息
                    xn = (XmlNode)xe;
                    break;
                case "IDEN_USERLIST"://获取用户信息列表
                    xn = (XmlNode)xe;
                    break;
                case "AUDIT_FETCH"://获取审计信息
                    xn = xe.SelectSingleNode("Logs");
                    break;
            }

            return xn;
        }
        #endregion
        #region 拼完整的信息包
        /// <summary>
        /// 拼完整的信息包
        /// 1.实例化一个requestdata对象
        /// 1.1实例化一个IdenManageOperationType对象
        /// 2.填充requestdata对象信息
        /// 3.拼写完整xml请求信息包
        /// </summary>
        /// <returns></returns>
        public override string GetWholeXML()
        {
            //1.实例化一个requestdata对象
            RequestData requestData = new RequestData();
            //1.1实例化一个IdenManageOperationType对象
            IdenManageOperationType imot = null;
            //2.填充requestdata对象信息
            requestData.TransactionID = System.Guid.NewGuid().ToString();
            requestData.RequestTime = System.DateTime.Now;
            requestData.AppName = TranPar.AppName;
            requestData.Token = System.Guid.NewGuid().ToString();
            requestData.ActionType = ActionType;
            switch (ActionType)
            {
                case ActionType.IDEN_MANAGE://身份信息管理
                    imot = new IdenManageOperationType();
                    switch (OperationType)
                    {
                        case OperationType.Create:
                            imot.OperationType = OperationType.Create;
                            break;
                        case OperationType.Delete:
                            imot.OperationType = OperationType.Delete;
                            break;
                        case OperationType.Disable:
                            imot.OperationType = OperationType.Disable;
                            break;
                        case OperationType.Enable:
                            imot.OperationType = OperationType.Enable;
                            break;
                        case OperationType.Update:
                            imot.OperationType = OperationType.Update;
                            break;
                    }
                   
                    break;
            }
            //3.拼写完整xml请求信息包
            StringBuilder xml = new StringBuilder();
            xml.Append(requestData.XMLBoday);
            if (imot != null)
            {
                xml.Append(imot.XML);
            }
            xml.Append(XmlInfo);
            xml.Append(requestData.XMLFinis);
            return xml.ToString() ;
        }
        #endregion
        #region 获取操作类型
        /// <summary>
        /// 获取操作类型
        /// </summary>
        /// <returns></returns>
        public ActionType GetActionType()
        {
            System.Xml.XmlElement ex = null;
            IAM.Interface.Model.ENUM.ActionType at = IAM.Interface.Model.ENUM.ActionType.Nullity;
            try
            {
                ex = XmlDocument.DocumentElement;
                string strActionType = ex.Attributes["ActionType"].InnerText;

                
                switch (strActionType)
                {
                    case "IDEN_MANAGE"://身份信息管理
                        at = ActionType.IDEN_MANAGE;
                        break;
                    case "IDEN_GETACMODEL"://获取权限模型
                        at = ActionType.IDEN_GETACMODEL;
                        break;
                    case "IDEN_GETUSERACI"://获取用户权限信息
                        at = ActionType.IDEN_GETUSERACI;
                        break;
                    case "IDEN_SETUSERACI"://设置用户权限信息
                        at = ActionType.IDEN_SETUSERACI;
                        break;
                    case "ORG_UPDATE"://组织信息更新
                        at = ActionType.ORG_UPDATE;
                        break;
                    case "ORG_CREATE"://组织机构创立
                        at = ActionType.ORG_CREATE;
                        break;
                    case "ORG_DELETE"://组织结构删除（包括合并）
                        at = ActionType.ORG_DELETE;
                        break;
                    case "DM_GETMODULELIST"://获取可代理模块列表
                        at = ActionType.DM_GETMODULELIST;
                        break;
                    case "DM_GETDELEINFO"://获取用户代理信息
                        at = ActionType.DM_GETDELEINFO;
                        break;
                    case "DM_SETDELEINFO"://设置用户代理信息
                        at = ActionType.DM_SETDELEINFO;
                        break;
                    default:
                        at = ActionType.Nullity;//无效
                        break;
                    //case "IDEN_USERLIST"://获取用户信息列表
                    //    xn = (XmlNode)xe;
                    //    break;
                    //case "AUDIT_FETCH"://获取审计信息
                    //    xn = xe.SelectSingleNode("Logs");
                    //    break;
                }
            }
            catch
            {
                at = ActionType.Nullity;//无效
            }
            _actiontype = at;
            return at;
        }
        #endregion
        #region 获取身份管理类型
        /// <summary>
        /// 获取身份管理类型
        /// </summary>
        /// <returns></returns>
        public OperationType GetOperationType()
        {
            System.Xml.XmlElement ex = XmlDocument.DocumentElement;
            string strActionType = ex.Attributes["ActionType"].InnerText;


            IAM.Interface.Model.ENUM.OperationType ot = IAM.Interface.Model.ENUM.OperationType.Create;
            if (strActionType == "IDEN_MANAGE")
            {
                XmlNode xn = ex.SelectSingleNode("OperationType");
                string strOperationType = xn.InnerText.Trim();
                
                switch (strOperationType)
                {
                    case "Create":
                        ot = IAM.Interface.Model.ENUM.OperationType.Create;
                        break;
                    case "Enable":
                        ot = IAM.Interface.Model.ENUM.OperationType.Enable;
                        break;
                    case "Disable":
                        ot = IAM.Interface.Model.ENUM.OperationType.Disable;
                        break;
                    case "Delete":
                        ot = IAM.Interface.Model.ENUM.OperationType.Delete;
                        break;
                    case "Update":
                        ot = IAM.Interface.Model.ENUM.OperationType.Update;
                        break;
                }
            }
            else
            {
                ot= IAM.Interface.Model.ENUM.OperationType.Nullity;
            }
            _operationtype = ot;
            return ot;
        }
        #endregion
        #region 获取请求的事务ID
        /// <summary>
        /// 获取请求的事务ID
        /// </summary>
        /// <returns></returns>
        public string GetTransactionID()
        {
            try
            {
                System.Xml.XmlElement ex = XmlDocument.DocumentElement;
                _transactionid = ex.Attributes["TransactionID"].InnerText;
                return ex.Attributes["TransactionID"].InnerText;
            }
            catch
            {
                return "";
            }
        }
        #endregion
        #endregion
    }
}
